package com.xp.flower.qsd.utils.weixin.pay.config;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;

public class WeChatConfig  implements Serializable{
    
  private static final long serialVersionUID = -9149413419019922968L;

  private static WeChatConfig instance;
  
  private String appId;
  private String mchId;
  private String apiKey;
  private String ufdoderUrl;
  private String notifyUrl;
  public  String tradeType;
  private String createIp;
  
  public static WeChatConfig getInstance(){
	  if(instance==null){
		  instance = new WeChatConfig();
	  }
	  return instance;
  }
  
  
  
	public WeChatConfig(){
		  this.initialize();
	}
	public String getAppId() {
		return appId;
	}
	public void setAppId(String appId) {
		this.appId = appId;
	}
	public String getMchId() {
		return mchId;
	}
	public void setMchId(String mchId) {
		this.mchId = mchId;
	}
	public String getUfdoderUrl() {
		return ufdoderUrl;
	}
	public void setUfdoderUrl(String ufdoderUrl) {
		this.ufdoderUrl = ufdoderUrl;
	}
	public String getNotifyUrl() {
		return notifyUrl;
	}
	public void setNotifyUrl(String notifyUrl) {
		this.notifyUrl = notifyUrl;
	}
	public String getCreateIp() {
		return createIp;
	}
	public void setCreateIp(String createIp) {
		this.createIp = createIp;
	}
	
	public String getTradeType() {
		return tradeType;
	}
	public void setTradeType(String tradeType) {
		this.tradeType = tradeType;
	}
	
	public String getApiKey() {
		return apiKey;
	}
	public void setApiKey(String apiKey) {
		this.apiKey = apiKey;
	}
	public void initialize(){
		  InputStream inputStream = null;
	  	try {
	  		inputStream 		= 	Thread.currentThread().getContextClassLoader().getResourceAsStream("config"+File.separator +"wechatpay.properties");
	  		Properties props	=	new Properties();
	  		props.load(new InputStreamReader(inputStream, "UTF-8"));
	  		// 设置参数
	  		setProperties(props);
	  		
	  	} catch (IOException e) 
	  	{
	  		throw new RuntimeException("Error loading properties file.", e);
	  	} finally 
	  	{
	  		if (inputStream != null) 
	  		{
	  			try 
	  			{
	  				inputStream.close();
	  			} catch (IOException e) 
	  			{
	  				e.printStackTrace();
	  			}
	  		}
	  	}
	  }
	  private void setProperties(Properties props){
		  if(props == null || props.isEmpty()) return;
		  appId=props.getProperty("appId");
		  mchId=props.getProperty("mchId");
		  ufdoderUrl=props.getProperty("ufdoder.url");
		  notifyUrl=props.getProperty("notify.url");
		  tradeType=props.getProperty("trade.type");
		  apiKey=props.getProperty("api.key");
		  setLocalIp();
	  }
    //设置本地ip
	private  void setLocalIp(){
      Enumeration allNetInterfaces;
      try 
      {
          allNetInterfaces = NetworkInterface.getNetworkInterfaces();
          
          while (allNetInterfaces.hasMoreElements()) 
          {
              NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
              List<InterfaceAddress> InterfaceAddress = netInterface.getInterfaceAddresses();
              
              for (InterfaceAddress add : InterfaceAddress) 
              {
                  InetAddress Ip = add.getAddress();
                  
                  if (Ip != null && Ip instanceof Inet4Address) 
                  {
                	  createIp = Ip.getHostAddress();
                  }
              }
          }
      } 
      catch (SocketException e) 
      {
          e.printStackTrace();
      }
      
  }
}
